
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BStyle>

#include "EdgeMergeBoard.h"
#include "MeshPanel.h"

MeshPanel::MeshPanel(BWidget* parent) : BGroupBox(parent)
{
	this->setTitle("Mesh");

	BHBoxLayout* topLayout = new BHBoxLayout();
	{
		_optimizedCheckBox = new BCheckBox("optimized");
		_normalSmoothButton = new BButton("smooth");
		_normalSmoothButton->setForm(Form_IconAlone);
		_facesReverseButton = new BButton("reverse");
		_facesReverseButton->setForm(Form_IconAlone);
		_edgeMergeButton = new BButton("edge-merge");
		_edgeMergeButton->setForm(Form_IconAlone);
		_edgeMergeButton->setCheckable(true);
		topLayout->addWidget(_optimizedCheckBox);
		topLayout->addWidget(_normalSmoothButton, 0);
		topLayout->addWidget(_facesReverseButton, 0);
		topLayout->addWidget(_edgeMergeButton, 0);
	}

	BGridLayout* gridLayout = new BGridLayout();
	{
		_vertexCountLabel = new BLabel();
		_normalCountLabel = new BLabel();
		_colorCountLabel = new BLabel();
		_texCoordCountLabel = new BLabel();
		_facesCountLabel = new BLabel();

		gridLayout->addWidget(1, 0, new BLabel("vertex"));
		gridLayout->addWidget(1, 1, _vertexCountLabel);
		gridLayout->addWidget(2, 0, new BLabel("normal"));
		gridLayout->addWidget(2, 1, _normalCountLabel);
		gridLayout->addWidget(3, 0, new BLabel("color"));
		gridLayout->addWidget(3, 1, _colorCountLabel);
		gridLayout->addWidget(4, 0, new BLabel("tex-coord"));
		gridLayout->addWidget(4, 1, _texCoordCountLabel);
		gridLayout->addWidget(5, 0, new BLabel("faces"));
		gridLayout->addWidget(5, 1, _facesCountLabel);
		gridLayout->setColumnStretch(0, 0);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(topLayout);
	vlayout->addLayout(gridLayout);

	connect(this, Signal_AssetChanged, &MeshPanel::slotAssetChanged);
	connect(_optimizedCheckBox, Signal_Toggled, &MeshPanel::slotOptimizedToggled);
	connect(_normalSmoothButton, Signal_Clicked, &MeshPanel::slotNormalSmoothButtonClicked);
	connect(_facesReverseButton, Signal_Clicked, &MeshPanel::slotFacesReverseButtonClicked);
	connect(_edgeMergeButton, Signal_Toggled, &MeshPanel::slotEdgeMergeButtonToggled);

}
MeshPanel::~MeshPanel()
{

}

void MeshPanel::slotAssetChanged(BObject* object, const BValue& value)
{
	BMesh* mesh = value;
	if (_mesh != mesh)
	{
		_optimizedCheckBox->setChecked(false);
		_vertexCountLabel->setText(BString());
		_normalCountLabel->setText(BString());
		_colorCountLabel->setText(BString());
		_texCoordCountLabel->setText(BString());
		_facesCountLabel->setText(BString());
		_mesh = mesh;
		if (_mesh)
		{
			_optimizedCheckBox->setChecked(_mesh->optimized());
			if (const BVectorArray* vertices = _mesh->vertices())
				_vertexCountLabel->setText(vertices->size());
			if (const BVectorArray* normals = _mesh->normals())
				_normalCountLabel->setText(normals->size());
			if (const BColorArray* colors = _mesh->colors())
				_colorCountLabel->setText(colors->size());
			if (const BCoordArray* coords = _mesh->texCoords())
				_texCoordCountLabel->setText(coords->size());
			if (const BFaceArray* faces = _mesh->faces())
				_facesCountLabel->setText(faces->size());
		}
	}
}
void MeshPanel::slotOptimizedToggled(BObject* object, const BValue& value)
{
	bool checked = value;
	_mesh->setOptimized(checked);
}
void MeshPanel::slotNormalSmoothButtonClicked(BObject* object, const BValue& value)
{
	_mesh->smooth();
}
void MeshPanel::slotFacesReverseButtonClicked(BObject* object, const BValue& value)
{
	_mesh->reverse();
}
void MeshPanel::slotEdgeMergeButtonToggled(BButton* button, const BValue& value)
{
	bool checked = value;
	static EdgeMergeBoard* board = new EdgeMergeBoard(this);
	if (checked)
	{
		board->setIcon(button->icon());
		board->setAsset(_mesh);
		board->popup();
		connect(board, Signal_Close, &MeshPanel::slotEdgeMergeBoardClosed);
	}
	else
	{
		board->hide();
	}
}
void MeshPanel::slotEdgeMergeBoardClosed(BButton* button, const BValue& value)
{
	_edgeMergeButton->setChecked(false);
}

void MeshPanel::styleEvent(const BEvent& event)
{
	BGroupBox::styleEvent(event);
	const BStyle* style = event.value();
	_normalSmoothButton->setIcon(style->icon("normal-smooth"));
	_facesReverseButton->setIcon(style->icon("faces-reverse"));
	_edgeMergeButton->setIcon(style->icon("edge-merge"));

	_normalSmoothButton->setTips(style->text("tips-normal-smooth"));
	_facesReverseButton->setTips(style->text("tips-faces-reverse"));
	_edgeMergeButton->setTips(style->text("tips-edge-merge"));
}
